/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditTaskDialog.java
 *
 * Created on 01/08/2010, 23:46:41
 */

package gui;

import base.Task;
import base.WeekDays;
import exception.RepositoryException;
import exception.TaskNotFoundException;
import facade.Facade;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author tacio
 */
public class EditTaskDialog extends javax.swing.JDialog {

    /** Creates new form EditTaskDialog */
    public EditTaskDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelTaskName = new javax.swing.JLabel();
        jTextFieldTaskName = new javax.swing.JTextField();
        jLabelSearchString = new javax.swing.JLabel();
        jTextFieldSearchString = new javax.swing.JTextField();
        jLabelShowSchedule = new javax.swing.JLabel();
        jCheckBoxMondays = new javax.swing.JCheckBox();
        jCheckBoxWednesdays = new javax.swing.JCheckBox();
        jCheckBoxFridays = new javax.swing.JCheckBox();
        jCheckBoxSundays = new javax.swing.JCheckBox();
        jCheckBoxSaturdays = new javax.swing.JCheckBox();
        jCheckBoxThursdays = new javax.swing.JCheckBox();
        jCheckBoxTuesdays = new javax.swing.JCheckBox();
        jPanel5 = new javax.swing.JPanel();
        jLabelChooseRssFeeds = new javax.swing.JLabel();
        jRadiobuttonEzRSSFeed = new javax.swing.JRadioButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Task");

        jLabelTaskName.setText("Task Name");

        jLabelSearchString.setText("Search String");

        jLabelShowSchedule.setText("Show Schedule (airs on)");
        jLabelShowSchedule.setToolTipText("For EZTV usually it's the show's name");

        jCheckBoxMondays.setText("mondays");

        jCheckBoxWednesdays.setText("wednesdays");

        jCheckBoxFridays.setText("fridays");

        jCheckBoxSundays.setForeground(new java.awt.Color(255, 0, 0));
        jCheckBoxSundays.setText("sundays");

        jCheckBoxSaturdays.setForeground(new java.awt.Color(255, 0, 0));
        jCheckBoxSaturdays.setText("saturdays");

        jCheckBoxThursdays.setText("thurdays");

        jCheckBoxTuesdays.setText("tuesdays");

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabelChooseRssFeeds.setText("Choose RSS Feed");
        jLabelChooseRssFeeds.setAlignmentY(0.0F);

        jRadiobuttonEzRSSFeed.setSelected(true);
        jRadiobuttonEzRSSFeed.setText("http://www.ezrss.it/feed");
        jRadiobuttonEzRSSFeed.setToolTipText("<html>EZRSS, the RSS feed for EZTV");
        jRadiobuttonEzRSSFeed.setActionCommand("<html>http://eztv.it/feed");
        jRadiobuttonEzRSSFeed.setAlignmentY(0.0F);
        jRadiobuttonEzRSSFeed.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelChooseRssFeeds)
                    .addComponent(jRadiobuttonEzRSSFeed))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelChooseRssFeeds)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadiobuttonEzRSSFeed)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldTaskName, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelTaskName)
                            .addComponent(jLabelSearchString)
                            .addComponent(jTextFieldSearchString, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelShowSchedule)
                            .addComponent(jCheckBoxSundays)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBoxWednesdays)
                                    .addComponent(jCheckBoxMondays))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBoxThursdays)
                                    .addComponent(jCheckBoxTuesdays)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jCheckBoxFridays)
                                .addGap(42, 42, 42)
                                .addComponent(jCheckBoxSaturdays))))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonCancel, jButtonOK});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTaskName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTaskName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelSearchString)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearchString, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelShowSchedule)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxMondays)
                    .addComponent(jCheckBoxTuesdays))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxWednesdays)
                    .addComponent(jCheckBoxThursdays))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxFridays)
                    .addComponent(jCheckBoxSaturdays))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxSundays)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
       try {
            Facade facade = Facade.getFacade();
            Task task = facade.search(jTextFieldTaskName.getText());
            task.setSearchString(jTextFieldSearchString.getText());

            ArrayList<WeekDays> schedule = new ArrayList<WeekDays>();
            if (jCheckBoxFridays.isSelected())
                schedule.add(WeekDays.FRIDAY);
            if (jCheckBoxMondays.isSelected())
                schedule.add(WeekDays.MONDAY);
            if (jCheckBoxSaturdays.isSelected())
                schedule.add(WeekDays.SATURDAY);
            if (jCheckBoxSundays.isSelected())
                schedule.add(WeekDays.SUNDAY);
            if (jCheckBoxThursdays.isSelected())
                schedule.add(WeekDays.THURSDAY);
            if (jCheckBoxTuesdays.isSelected())
                schedule.add(WeekDays.TUESDAY);
            if (jCheckBoxWednesdays.isSelected())
                schedule.add(WeekDays.WEDNESDAY);

            task.setSchedule(schedule);
            facade.updateTask(task);
            JOptionPane.showMessageDialog(this, "Task updated with success!");
        } catch (RepositoryException ex) {
            Logger.getLogger(EditTaskDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TaskNotFoundException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonCancelActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                EditTaskDialog dialog = new EditTaskDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JCheckBox jCheckBoxFridays;
    private javax.swing.JCheckBox jCheckBoxMondays;
    private javax.swing.JCheckBox jCheckBoxSaturdays;
    private javax.swing.JCheckBox jCheckBoxSundays;
    private javax.swing.JCheckBox jCheckBoxThursdays;
    private javax.swing.JCheckBox jCheckBoxTuesdays;
    private javax.swing.JCheckBox jCheckBoxWednesdays;
    private javax.swing.JLabel jLabelChooseRssFeeds;
    private javax.swing.JLabel jLabelSearchString;
    private javax.swing.JLabel jLabelShowSchedule;
    private javax.swing.JLabel jLabelTaskName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JRadioButton jRadiobuttonEzRSSFeed;
    private javax.swing.JTextField jTextFieldSearchString;
    private javax.swing.JTextField jTextFieldTaskName;
    // End of variables declaration//GEN-END:variables

}
